package com.ipan.builder.var;

import java.util.HashMap;
import java.util.Map;

import com.ipan.builder.ognl.ValueStack;

/**
 * 变量操作基类
 * 
 * @author iPan
 * @version 2013-12-4
 */
public abstract class AbstractVariateOp implements VariateOp {

	protected String varScope;
	protected ValueStack vs;
	
	public AbstractVariateOp(ValueStack vs, String scope) {
		this.vs = vs;
		this.varScope = scope;
	}
	
	public void putAll(Map<String, Object> vals) {
		getContext().putAll(vals);
	}
	
	public void putVar(String key, Object value) {
		getContext().put(key, value);
	}
	
	public Object getVar(String key) {
		return getContext().get(key);
	}
	
	public void removeVar(String key) {
		getContext().remove(key);
	}
	
	@SuppressWarnings("unchecked")
	private Map<String, Object> getContext() {
		Map<String, Object> context = null;
		if (vs.findValue(varScope) != null) {
			context = ((Map<String, Object>) vs.findValue(varScope));
		} else {
			context = new HashMap<String, Object>();
			vs.setValue(varScope, context);
		}
		return context;
	}
}
